const os = require('os')
// 操作系统模块
console.log(`行定界符:\t${os.EOL}`)
console.log(`信号相关常量:\t${os.constants.signals.SIGABRT}`)
console.log(`错误相关常量:\t${os.constants.errno.E2BIG}`)
console.log(`底层架构:\t${os.arch()}`)
console.log(`CPU信息:\t${JSON.stringify(os.cpus())}`)
console.log(`大端序或小端序:\t${os.endianness()}`)
console.log(`内存字节数:\t${os.freemem()}`)
console.log(`当前用户主目录:\t${os.homedir()}`)
console.log(`当前主机名:\t${os.hostname()}`)
console.log(`系统平均负载:\t${os.loadavg()}`)
console.log(`网络接口信息:\t${JSON.stringify(os.networkInterfaces())}`)
console.log(`nodejs编译平台:\t${os.platform()}`)
console.log(`操作系统版本号:\t${os.release()}`)
console.log(`临时文件夹:\t${os.tmpdir()}`)
console.log(`可用总内存字节数:\t${os.totalmem()}`)
console.log(`标识操作系统:\t${os.type()}`)
console.log(`上次启动计算机持续运行秒数:\t${os.uptime()}`)
console.log(`当前用户信息:\t${JSON.stringify(os.userInfo())}`)

/*
buffer
*/
// buffer与编码
const buffer = Buffer.from('this is buffer.', 'ascii')
console.log('uft8:\t' + buffer.toString('utf8'))
console.log('uft16le:\t' + buffer.toString('utf16le'))
console.log('binary:\t' + buffer.toString('binary'))
console.log('hex:\t' + buffer.toString('hex'))
console.log('base64:\t' + buffer.toString('base64'))
console.log('latin1:\t' + buffer.toString('latin1'))
// 创建buffer
const buf1 = Buffer.alloc(10) //使用0进行初始化
const buf2 = Buffer.allocUnsafe(10) //不进行初始化
console.log(buf1.toString('utf8'))
console.log('------------')
console.log(buf2.toString('utf8'))
// 从数组创建
const buf3 = Buffer.from([98, 89, 77, 66, 44])
// 从字符串创建
const buf4 = Buffer.from('[98,89,77,66,44]')
// 访问数据
console.log(`${buf4[0]}-${buf4[1]}-${buf4[2]}`)
console.log(`${buf4.toString()}`)
console.log(`${buf4.toString('utf8',5)}`)
console.log(`buffer长度：\t${buf4.length}`)
// 迭代访问
for (const item of buf4) {
    console.log(item)
}
// 写入数据
buf4.write('ABC')
buf4.write('AB', 4, 2, 'utf8')
console.log(`${buf4.toString()}`)
buf4[0] = 93
console.log(`${buf4.toString()}`)
// 复制
let buf5 = Buffer.alloc(200)
buf4.copy(buf5)
console.log(`Buffer Copy:\t${buf5.toString()}`)
// 切片
const slice1 = buf4.slice(1, 6)
console.log(`slice1-6:\t ${slice1.toString()}`)
buf4.write('change', 1)
console.log(`slice1-6:\t ${slice1.toString()}`)
// 转换为json对象
console.log(buf4.toJSON())
console.log(JSON.parse(JSON.stringify(buf4.toJSON()), (k, v) => {
    return v && v.type == 'Buffer' ?
        Buffer.from(v.data) :
        v
}))
// 缓冲区合并
const buf6 = Buffer.concat([buf4, buf5])
console.log(buf6.toString())
/*
流
*/
// 文件读写
var fs = require('fs')
const file = './UseOs.txt'
var data = 'nodejs 文件读写 演示'
if (!fs.existsSync(file))
    var fd = fs.openSync(file, 'w+')
var wstream = fs.createWriteStream(file)
wstream.write(data, 'utf8')
wstream.end()
wstream.on('finish', () => {
    console.log('写入完成')
})
wstream.on('error', (err) => {
    console.log('写入错误' + err)
})
wstream.setDefaultEncoding('utf8')
// 读取
var rstream = fs.createReadStream(file)
rstream.setEncoding('utf8')
let rdata = ''
rstream.on('data', d => {
    rdata += d
})
rstream.on('end', () => {
    console.log('读取完成')
    console.log(rdata)
})
rstream.on('error', (err) => {
    console.log('读取错误' + err)
})
// 管道流
// 创建一个可读流
var readerStream = fs.createReadStream(file);
// 创建一个可写流
var writerStream = fs.createWriteStream(file + '.txt');
// 管道读写操作
// 读取 input.txt 文件内容，并将内容写入到 output.txt 文件中
readerStream.pipe(writerStream);
// 链式流
var zlib = require('zlib')
fs.createReadStream(file)
    .pipe(zlib.createGzip())
    .pipe(fs.createWriteStream(file + '.gz'))

let delFiles = [file, file + '.gz', file + '.txt', 'node_modulles_xxx']
for (const f of delFiles) {
    if (fs.existsSync(f))
        fs.rmSync(f)
}
let delDirs = ['node_modules_xxx']
for (const d of delDirs) {
    if (fs.existsSync(d))
        fs.rmdirSync(d)
}
//